package othello.neuralnetwork;

import java.util.Random;

public class testNeuralNetwork{

    public static void main(String[] args) {
        double inputs[][] = new double[20000][64];
        double targets[]  =  new double[20000];

        Random generator = new Random();
        for (int i = 0; i != 1000; i++) {
            targets[i] = 0.0;
            for (int j = 0; j != 64; j++) {
                inputs[i][j] = (double)generator.nextInt(2);
                targets[i] += inputs[i][j];
            }
        }
        
        for (int i = 0; i != 20000; i++){
            if (targets[i] < 32) {
                targets[i] = 1.0;
            } else {
                targets[i] = 0.0;
            }                
        }       

        NeuralNetwork nn = new NeuralNetwork(5,1);

        for (int i = 0; i != 1000000; i++){
            int idx = generator.nextInt(18000);
            nn.forwardPropagate(inputs[idx]);

            double error = targets[idx] - nn.getOutput()[0];
            nn.backPropagate(error,0.001,0);
            double squaredError = 0.0;                

            if (i%1 == 0){
                for (int j = 18000; j != 18010; j++) {
                    nn.forwardPropagate(inputs[j]);
                    double err = targets[j] - nn.getOutput()[0];
                    squaredError += err*err;
                }
                System.out.println("MSE: " + Math.sqrt(squaredError/100.0));
            }
        }
        
        double squaredError = 0.0;
        
        for (int j = 900; j != 910; j++){
            nn.forwardPropagate(inputs[j]);
            double err = targets[j] - nn.getOutput()[0];
            squaredError += err*err;
            System.out.println("Target: " + targets[j] + ", NN output: " + nn.getOutput()[0]);
        }
        System.out.println("MSE: " + Math.sqrt(squaredError/100.0));        
    }
}
